<?php
/**
 * Copyright (C), 2021-2021, Shall Buy Life info. Co., Ltd.
 * FileName: AuthData.php
 * Description: 数据编码
 *
 * @author lwl
 * @Create Date    2021/8/6 11:34
 * @Update Date    2021/8/6 11:34 By lwl
 * @version v1.0
 */

namespace Example\Helpers;

class AuthData
{
    private static $instance = null;
    private static $config = [];


    /**
     * FunctionName：getInstance
     * Description：
     * Author：lwl
     * @return AuthData|null
     */
    public static function getInstance()
    {
        if (is_null(self::$instance)) {
            self::$instance = new self();
        }
        return self::$instance;
    }

    /**
     * Verify constructor.
     */
    private function __construct()
    {
        self::$config = config('authdata');//获取对照字典
    }

    /**
     * FunctionName：__clone
     * Description：私有clone
     * Author：lwl
     */
    private function __clone()
    {

    }

    /**
     * FunctionName：__wakeup
     * Description：私有wakeup
     * Author：lwl
     */
    private function __wakeup()
    {

    }
    /**
     * FunctionName：encode
     * Description：编码
     * Author：lwl
     * @param string $char
     * @return string
     */
    public function encode(string $char): string
    {
        $char = base64_encode($char);
        $letters = self::$config['letter'];
        foreach ($letters as $key => $letter) {
            $char = str_replace($key, $letter, $char);
        }
        return $char . self::$config['suffix'];
    }

    /**
     * FunctionName：decode
     * Description：解码
     * Author：lwl
     * @param string $char
     * @return string
     */
    public  function decode(string $char): string
    {
        $char = str_replace(self::$config['suffix'], '', $char);
        $letters = array_flip(self::$config['letter']);
        foreach ($letters as $key => $letter) {
            $char = str_replace($key, $letter, $char);
        }
        return base64_decode($char);
    }
}
